@extends('Admin.Layouts.layout')
@section('title','后台管理系统')
@section('style')
    <style>

    </style>
@endsection
@section('content')
    <div class="lyl-content">
        <div class="am-cf admin-main2">
            <div class="admin-content">
                <div class="admin-content-body">
                    <div class="am-g am-animation-slide-right">
                        <form class="am-form am-form-horizontal" action="" method="post" id="from1" onsubmit="return handle()">
                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-12">
                                    <div class="am-u-sm-3  am-text-right">权限组名：</div>
                                    <div class="am-u-sm-4">
                                        <input type="text" class="am-input-sm" name="title" required  v-model="group.title" placeholder="请输入权限组名">
                                        <input type="hidden" name="group_id" v-model="group.group_id">
                                        <input type="hidden" name="rule" v-model="rule_val">
                                        {!! csrf_field() !!}
                                    </div>
                                    <div class="am-u-sm-4"></div>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-12">
                                    <div class="am-u-sm-3  am-text-right">权限描述：</div>
                                    <div class="am-u-sm-4">
                                        <input type="text" class="am-input-sm" name="miaoshu" required v-model="group.miaoshu" placeholder="请输入描述">
                                    </div>
                                    <div class="am-u-sm-4"></div>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-12">
                                    <div class="am-u-sm-3  am-text-right">拥有权限：</div>
                                    <div class="am-u-sm-9">
                                        <div class="am-form-group" v-for="item in cat">
                                            <label>@{{ item.name }}</label>
                                            <div class="am-form-group">
                                                <template v-for="rule in rule_all" v-if="rule.cat_id == item.cat_id">
                                                    <label class="am-checkbox-inline" v-if="">
                                                        <input type="checkbox" :value="rule.rule_id" v-model="rule_val">@{{ rule.desc }}
                                                    </label>
                                                    <label class="am-checkbox-inline" v-else>
                                                        <input type="checkbox" :value="rule.rule_id" v-model="rule_val" selected>@{{ rule.desc }}
                                                    </label>
                                                </template>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>

                            <div class="am-g am-margin-top">
                                <div class="am-u-sm-8 am-u-sm-offset-2">
                                    <button type="submit" class="am-btn am-btn-secondary" v-if="group.length===0">添加权限组</button>
                                    <button type="submit" class="am-btn am-btn-secondary" v-else>修改权限组</button>
                                </div>
                            </div>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('script')
    <script>
        var vm = new Vue({
            el:'.lyl-content', //挂载点：主体页面
            data:{
                group       : {!! $data['group'] or "[]" !!},   //当前权限组
                cat         : {!! $data['cat'] !!},             //所有权限分类
                rule_all    : {!! $data['rule'] !!},            //所有权限
                rule_val    : {!! (!empty($data['rule_val']) && $data['rule_val']!="false" && $data['rule_val']!="true")?$data['rule_val']:"[]" !!} //当前权限
            }
        });

        function handle(){
            $.post("{{ url("admin/admingroup/handle") }}",$("#from1").serialize(),function (data) {
                if(data['status'] == "200"){
                    layer.msg(data['text'],{icon:1,time:1000},function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.location.reload()
                        parent.layer.close(index);
                    })
                }else{
                    layer.msg(data['text'],{icon:2,time:1000});
                }
            });
            return false;
        }
    </script>
@endsection